﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using Chinesys.UI;
using Chinesys.Domain.InputValidations;
using Chinesys.Entities;
using Chinesys.UI.Helpers;
using Chinesys.Domain;
using Chinesys.Domain.BizRules;

namespace Chinesys.UI.Account
{
    public partial class Register : PageBase
    {

        protected void Page_Load(object sender, EventArgs e)
        {
            Master.PageTitle = "Nuevos Usuario";
        }

        protected void btnCreateAccount_Click(object sender, EventArgs e)
        {
            List<InputValidationError> errors = new List<InputValidationError>();
            UserEntity user = new UserEntity();

            user.Address = TypeHelper.ConvertToType<string>(txtAddress.Text, lblAddress.Text, errors);
            user.Email = TypeHelper.ConvertToType<string>(txtEmail.Text, lblEmail.Text, errors);
            user.FirstName = TypeHelper.ConvertToType<string>(txtFirstName.Text, lblFirstName.Text, errors);
            user.LastName = TypeHelper.ConvertToType<string>(txtLastName.Text, lblLastName.Text, errors);
            user.Newsletter = chkNewsletter.Checked;
            user.Pass = TypeHelper.ConvertToType<string>(txtPassword.Text, lblPassword.Text, errors);
            user.PhoneOne = TypeHelper.ConvertToType<string>(txtPhone.Text, lblPhone.Text, errors);
            user.UserName = TypeHelper.ConvertToType<string>(txtUserName.Text, lblUserName.Text, errors);
            user.Active = true;
            
            if(string.IsNullOrEmpty(txtPassword.Text)  || string.IsNullOrEmpty(txtConfirmPassword.Text))
                errors.Add(new GeneralValidation("DebeIngresar el password y la confirmacion de la misma."));

            if (txtPassword.Text != txtConfirmPassword.Text)
                errors.Add(new GeneralValidation("The password does not match confirmation."));

            if (errors.Any())
            {
                validationSummary.Errors = errors;
            }
            else
            {
                lblInfo.Visible = false;
                validationSummary.Clear();

                try
                {
                    user.CurrentState = RowState.New;
                    UserDomain.SaveCustomerAndCreateAccount(user);

                    lblInfo.Text = @"Se ha registrado exitosamente. Nuestro departamento de cuentas corrientes verificara sus datos 
                                     y le enviara un mail confirmandole que su cuenta a sido activada y cual sera su limite de compra.";

                    lblInfo.Visible = true;
                    pnlRegister.Visible = false;
                }
                catch (InputValidationException ex)
                {
                    validationSummary.Errors = ex.Validator.Errors;
                }
                catch (BizRulesException ex)
                {
                    hdnBizRulesException.Value = ex.Message;
                }
            }

        }
    }
}

